package cn.openea.perpartner.meily.web.mapper;

import cn.openea.perpartner.meily.web.entity.BuyingRecord;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * (BuyingRecord)表数据库访问层
 *
 * @author rains
 * @since 2023-02-09 17:18
 */
@Repository
public interface BuyingRecordMapper extends BaseMapper<BuyingRecord> {

    @Select("select ifnull(sum(buy_quantity),0) from meily_buying_record where 1=1 ${ew.customSqlSegment}")
    Long getGoodsSalesVolume(@Param("ew") QueryWrapper<BuyingRecord> wrapper);

    /**
     * 获取当前月（不含当天）所有购买记录
     * @return
     */
    @Select("select * from meily_buying_record where DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') and create_time < date_sub(DATE_FORMAT(now(), '%Y-%m-%d 23:59:59'), INTERVAL 1 day)")
    List<BuyingRecord> getCurrentMonthList();

    /** **************************** 2023-05-04 add by lys start *************************************** */

    /**
     * 获取商品总销量
     * 商品总销量 = 自定义销量 + 实际销量
     * @return 查询结果
     */
    @Select("select ifnull(sum(buy_quantity),0) from meily_buying_record where 1=1 ${ew.customSqlSegment}")
    long getTotalSales(@Param("ew") LambdaQueryWrapper<BuyingRecord> wrapper);

    /** **************************** 2023-05-04 add by lys end *************************************** */

}
